#ifndef MAINFRAME_H
#define MAINFRAME_H

#include <wx/frame.h>
#include <wx/listctrl.h>
#include <wx/button.h>
#include <wx/image.h>
#include <wx/panel.h>
#include <wx/radiobut.h>
#include <wx/slider.h>
#include <wx/filepicker.h>
#include <wx/stattext.h>
#include <wx/msgdlg.h>
#include <wx/timer.h>
#include <wx/sizer.h>
#include <wx/msgdlg.h>
#include <fmt/format.h>
#include <filesystem>
#include <fstream>
#include <sstream>
#include "cameraDriver.h"
#include "ImageDisplay.h"
#include "CameraControl.h"
#include "ImagePlayEngine.h"
#include "SaveFile.h"
#include "AnalyControlBar.h"
#include "WebMonitor.h"
#include "RemoteHandle.h"

enum class MainFrameStatus;

class MainFrame : public wxFrame
{
public:
	MainFrame(cameraDriver* _CameraDriver);
	~MainFrame();
private:
	friend class AnalyControlBar;
	friend class ImagePlayEngine;

	wxButton* btnRefreshDevice;
	wxButton* btnOpenCloseDevice;
	wxButton* btnControlCollege;
	wxButton* btnVideoPlayControl;
	wxButton* btnUpdateDir;
	wxButton* btnOutputToBMP;
	wxButton* btnCameraShot;
	wxStaticText* txtInf;
	wxDirPickerCtrl* dirpSaveDir;
	ImageDisplay* panelCameraDisplay;
	CameraControl* panelCameraControl;
	wxListCtrl* listDeviceList;
	wxListCtrl* listVideoList;
	wxSlider* sliderPlayControl;
	wxSlider* sliderPlaySpeedControl;
	wxStaticText* txtPlayFrameInf;
	wxStaticText* txtPlayFrameCount;
	AnalyControlBar* analyBar;
	wxRadioButton* radioPlayMode;
	wxRadioButton* radioAnalyMode;
	wxButton* btnRemoteSetting;
	wxCheckBox* cboxRemoteMonitor;
	wxMessageDialog* msgFileTransfer;

	wxTimer* InfUpdate;
	friend void on_VideoPlayFinish(void* target);

	cameraDriver* CameraDriver;
	std::list<cameraDriver::cameraDevice>::iterator devicePtr;
	ImagePlayEngine* playEngine;
	SaveFile saveFile;
	RemoteHandle remoteHandle;
	std::string currentVideoFile;
	
	//Event Handle
	void on_radioPlayMode_click(wxCommandEvent& event);
	void on_radioAnalyMode_click(wxCommandEvent& event);
	void on_btnCameraShot_click(wxCommandEvent& event);
	void on_btnRefreshDevice_click(wxCommandEvent& event);
	void on_btnOpenCloseDevice_click(wxCommandEvent& event);
	void on_listDeviceList_select(wxListEvent& event);
	void on_dirpSaveDir_change(wxFileDirPickerEvent& event);
	void on_btnControlCollege_click(wxCommandEvent& event);
	void on_InfUpdate_timeout(wxTimerEvent& event);
	void on_btnVideoPlayControl_click(wxCommandEvent& event);
	void on_listVideoList_select(wxListEvent& event);
	void on_btnUpdateDir_click(wxCommandEvent& event);
	void on_sliderPlaySpeedControl_change(wxScrollEvent& event);
	void on_sliderPlayControl_change(wxScrollEvent& event);
	void on_btnOutputToBMP_click(wxCommandEvent& event);
	void on_btnRemoteSetting_click(wxCommandEvent& event);
	void on_cboxRemoteMonitor_click(wxCommandEvent& event);
	//用于处理一些没有启动的功能
	void on_unableFunc(wxEvent& event);
	//为异步调用准备的发送函数
	void emitPlayEngineFinish();
	void emitSetPlayPos(int pos);
	//远程事件
	void on_RemoteGetStatus(wxCommandEvent& event);
	void on_RemoteStartShot(wxCommandEvent& event);
	void on_RemoteStopShot(wxCommandEvent& event);
	void on_RemoteInitDevice(wxCommandEvent& event);
	void on_RemoteStartSave(wxCommandEvent& event);
	void on_RemoteStopSave(wxCommandEvent& event);
	void on_RemoteGetCameraPara(wxCommandEvent& event);
	void on_RemoteEnableTriggerMode(wxCommandEvent& event);
	void on_RemoteDisableTriggerMode(wxCommandEvent& event);
	void on_RemoteSetExposeTime(RemoteCommandAndParament& event);
	void on_RemoteSetImageHeight(RemoteCommandAndParament& event);
	void on_RemoteSetImageWidth(RemoteCommandAndParament& event);
	void on_RemoteSetOffsetX(RemoteCommandAndParament& event);
	void on_RemoteSetOffsetY(RemoteCommandAndParament& event);
	void on_RemoteSetFrameRate(RemoteCommandAndParament& event);
	void on_RemoteFileTransferEnable(wxCommandEvent& event);
	void on_RemoteFileTransferDisable(wxCommandEvent& event);
	void on_RemoteFileTransferRequest(wxCommandEvent& event);
	bool RemoteCommandErrorFlag=false;
	//检查图像采集功能是否可用
	void checkImageSaveFunc();
	//检查文件播放和分析功能是否可用
	void checkFilePlayAnalyizeFunc();
	//检查文件是否打开，如果没有打开就自动打开一份文件
	bool checkFileReady();
	//设置当前程序状态以便确定UI,返回一个状态是否设置成功
	bool setStatus(MainFrameStatus status);
	//检查配置文件
	void CheckConfigFile();
	//检查相机参数是否设置成功
	inline void CheckCameraParamentSetting(const std::function<void()>& func);
	//检查相机状态是否正常
	inline bool CheckStatus(const MainFrameStatus targetStatus);
	//获取是否连接到服务器
	bool GetServerLinkStatus();
	
	MainFrameStatus mStatus;

};

enum class MainFrameStatus
{
	Init,
	VideoPlay,
	ImageCollege,
	CameraRunning,
	Noting,
	ImageAnalysis
};

enum class MainFrameID : int
{
	btnRefreshDevice,
	btnOpenCloseDevice,
	btnControlCollege,
	btnVideoPlayControl,
	dirpSaveDir,
	panelCameraDisplay,
	panelCameraControl,
	listDeviceList,
	sliderPlayControl,
	sliderPlaySpeedControl,
	InfUpdate,
	listVideoList,
	btnUpdateDir,
	txtPlayFrameCount,
	txtPlayFrameInf,
	btnOutputToBMP,
	btnCameraShot,
	listFrameGrep,
	radioPlayMode,
	radioAnalyMode,
	playEngine,
	btnRemoteSetting,
	cboxRemoteMonitor
};

#endif